home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 June / PCpro_2005_06.ISO / files / opensource / amc / amc_install.exe / {app} / Scripts / Amazon (large pic).ifs < prev    next >
Encoding:
Text File  |  2005-01-13  |  7.1 KB  |  210 lines

  1. (***************************************************
  2.  
  3. Ant Movie Catalog importation script
  4. www.antp.be/software/moviecatalog/
  5.  
  6. [Infos]
  7. Authors=polnjumen (polnjumen@yahoo.com)
  8. Title=Amazon
  9. Description=Import picture & editorial review from Amazon
  10. Site=www.amazon.com
  11. Language=EN
  12. Version=1.0
  13. Requires=3.5.0
  14. Comments=Based on the script made for version 3.4.0 by facts (jfacts1731@yahoo.com)
  15. License=This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  16. GetInfo=1
  17.  
  18. [Options]
  19. GetReview=2|1|0=Do not get Editorial Review|1=Save Editorial Review into Description field|2=Save Editorial Review into Comment field
  20.  
  21. ***************************************************)
  22.  
  23. program Amazon;
  24.  
  25. uses
  26.   StringUtils1;
  27.  
  28. var
  29.   AmazonPage: TStringList;
  30.   THolder, MovieName : string;
  31.   LineNr, I: Integer;
  32.  
  33. function FindLine(Pattern: string; List: TStringList; StartAt: Integer): Integer;
  34. var
  35.   i: Integer;
  36. begin
  37.   Result := -1;
  38.   if StartAt < 0 then
  39.     StartAt := 0;
  40.   for i := StartAt to List.Count-1 do
  41.     if Pos(Pattern, List.GetString(i)) <> 0 then
  42.     begin
  43.       Result := i;
  44.       Break;
  45.     end;
  46. end;
  47.  
  48. function GetToken(aString, SepChar: String; TokenNum: Integer):String;
  49. var
  50.    Token     : string;
  51.    StrLen    : Integer;
  52.    TNum      : Integer;
  53.    TEnd      : Integer;
  54.  
  55. begin
  56.      StrLen := Length(aString);
  57.      TNum   := 1;
  58.      TEnd   := StrLen;
  59.      while ((TNum <= TokenNum) and (TEnd <> 0)) do
  60.      begin
  61.           TEnd := Pos(SepChar,aString);
  62.           if TEnd <> 0 then
  63.           begin
  64.                Token := Copy(aString,1,TEnd-1);
  65.                Delete(aString,1,TEnd);
  66.                TNum := TNum + 1;
  67.           end
  68.           else
  69.           begin
  70.                Token := aString;
  71.           end;
  72.      end;
  73.      if TNum >= TokenNum then
  74.      begin
  75.           GetToken := Token;
  76.      end
  77.      else
  78.      begin
  79.           GetToken := '';
  80.      end;
  81. end;
  82.  
  83. function AsinParse(Line : string): string;
  84. begin
  85.   Result := GetToken(GetToken(Line,'.',2),Chr(34),1);
  86. end;
  87.  
  88. // ***** analyze the page containing movie information *****
  89.  
  90. procedure AnalyzeMoviePage(AsinNr: string);
  91. var
  92.   MovieURL, PageText, Value, Value2: string;
  93.   LP: integer;
  94. begin
  95.   GetPicture('http://images.amazon.com/images/P/' + AsinNr + '.01.LZZZZZZZ.jpg');
  96.   MovieURL := 'http://www.amazon.com/exec/obidos/tg/detail/-/' + AsinNr + '/';
  97.   PageText := GetPage(MovieURL);
  98.   // URL
  99.   if CanSetField(fieldURL) then
  100.     SetField(fieldURL, MovieURL);
  101.   // DVD Title & Year
  102.   if CanSetField(fieldOriginalTitle) or CanSetField(fieldYear) then
  103.   begin
  104.     Value := TextBetween(PageText, '<title>', '</title>');
  105.     LP := LastPos('(', Value);
  106.     Value2 := Copy(Value, LastPos('(', Value) + 1, 4);
  107.     if CanSetField(fieldYear) then
  108.       SetField(fieldYear, Value2);
  109.     Value2 := TextBetween(Value, 'Amazon.com: DVD: ',' ('+ Value2);
  110.     HTMLDecode(Value2);
  111.     if CanSetField(fieldOriginalTitle) then
  112.       SetField(fieldOriginalTitle, Value2);
  113.   end;
  114.   //Editorial Review
  115.   if GetOption('GetReview') > 0 then
  116.   begin
  117.     Value := TextAfter(PageText, '<a name="amzn-reviews">');
  118.     if Value <> '' then
  119.     begin
  120.       Value := TextBetween(Value, '<b class=h1>Editorial Reviews</b>', '<a name="cust-reviews-section">');
  121.       Value := StringReplace(Value, #13#10, ' ');
  122.       Value := StringReplace(Value, '<br>', #13#10);
  123.       HTMLRemoveTags(Value);
  124.       HTMLDecode(Value);
  125.       Value := Trim(Value);
  126.       while Pos('  ', Value) > 0 do
  127.         Value := StringReplace(Value, '  ', ' ');
  128.       while Pos(#13#10, Value) = 1 do
  129.         Delete(Value, 1, 2);
  130.       case GetOption('GetReview') of
  131.         1:
  132.           if CanSetField(fieldDescription) then
  133.             SetField(fieldDescription, Value);
  134.         2:
  135.           if CanSetField(fieldComments) then
  136.             SetField(fieldComments, Value);
  137.       end;
  138.     end;
  139.   end;
  140.   // Director
  141.   if CanSetField(fieldDirector) then
  142.   begin
  143.     Value := TextBetween(PageText, '<b>Director:</b>','</a><br>');
  144.     HTMLRemoveTags(Value);
  145.     HTMLDecode(Value);
  146.     SetField(fieldDirector, Trim(Value));
  147.   end;
  148.   // Producer
  149.   if CanSetField(fieldProducer) then
  150.   begin
  151.     Value := TextBetween(PageText, '<b>Studio:</b>','<li>');
  152.     Value := StringReplace(Value, #13#10, '');
  153.     SetField(fieldProducer, Trim(Value));
  154.   end;
  155.   // Length
  156.   if CanSetField(fieldLength) then
  157.   begin
  158.     Value := TextBetween(PageText, '<b>Run Time:</b>','<br>');
  159.     SetField(fieldLength, Trim(Value));
  160.   end;
  161. end;
  162.  
  163. // ***** beginning of the program *****
  164.  
  165. begin
  166.   if CheckVersion(3,5,0) then
  167.   begin
  168.     AmazonPage := TStringList.Create;
  169.     AmazonPage.Text := GetPage('http://www.amazon.com/exec/obidos/search-handle-url/index=dvd&field-title=' + StringReplace(UrlEncode(GetField(fieldOriginalTitle)),'+', '%20'));
  170.     if FindLine('unable to find exact matches',AmazonPage,1) <> -1 then
  171.       begin
  172.         ShowMessage('Movie "' + GetField(fieldOriginalTitle) + '" not found.');
  173.         exit;
  174.       end
  175.     if FindLine('Amazon.com: DVD:',AmazonPage,1) <> -1 then
  176.       begin
  177.         LineNr := FindLine('<input type="hidden" name="asin.',AmazonPage,1);
  178.         THolder := AsinParse(AmazonPage.Getstring(LineNr));
  179.         AnalyzeMoviePage(THolder);
  180.         exit;
  181.       end
  182.     else
  183.       if (FindLine('Below are results for',AmazonPage,1) <> -1) OR (FindLine('All results',AmazonPage,1) <> -1) OR (FindLine('Most popular results for',AmazonPage,1) <> -1) OR (FindLine('Results for',AmazonPage,1) <> -1) then
  184.         begin
  185.           i := 1;
  186.           PickTreeClear;
  187.           PickTreeAdd('Movies','');
  188.           while (i <= AmazonPage.Count-1) do
  189.             begin
  190.               THolder := AmazonPage.GetString(i);
  191.               HTMLRemoveTags(THolder);
  192.               if (Pos('/exec/obidos/ASIN/',AmazonPage.GetString(i)) <> 0) and (THolder <> '') and (Pos('Buy new',AmazonPage.GetString(i)) = 0) and (Pos('Used & new from',AmazonPage.GetString(i)) = 0) and (Pos('THUMBZZZ',AmazonPage.GetString(i)) = 0) then
  193.                 PickTreeAdd(THolder,GetToken(AmazonPage.GetString(i),'/',5));
  194.               if (Pos('/exec/obidos/tg/detail/',AmazonPage.GetString(i)) <> 0) and (THolder <> '') and (Pos('Buy new',AmazonPage.GetString(i)) = 0) and (Pos('Used & new from',AmazonPage.GetString(i)) = 0) and (Pos('THUMBZZZ',AmazonPage.GetString(i)) = 0) and (Pos('http://www.amazon.com',AmazonPage.GetString(i)) = 0) and (Pos('In-store Pickup',AmazonPage.GetString(i)) = 0) then
  195.                 PickTreeAdd(THolder,GetToken(AmazonPage.GetString(i),'/',7));
  196.               i := i + 1;
  197.             end
  198.           if PickTreeExec(THolder) then
  199.             begin
  200.               AnalyzeMoviePage(THolder);
  201.             end
  202.           exit;
  203.         end
  204.       else
  205.         ShowMessage('Movie "' + GetField(fieldOriginalTitle) + '" not found.');
  206.     end
  207.   else
  208.     ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.5.0)');
  209. end.
  210.